using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class ViewOrderDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

        public ViewOrderDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods		

		/// <summary>
        /// Selects all records from the ViewMaterial table.
		/// </summary>
        public List<ViewOrderDTO> SelectAll()
		{
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewOrderSelectAll"))
			{
                List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
				while (dataReader.Read())
				{
                    ViewOrderDTO viewOrderDTO = MapDataReader(dataReader);
                    viewOrderDTOList.Add(viewOrderDTO);
				}

                return viewOrderDTOList;
			}
		}

       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
        public List<ViewOrderDTO> SelectComplete()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewOrderSelectComplete"))
            {
                List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
                while (dataReader.Read())
                {
                    ViewOrderDTO viewOrderDTO = MapDataReader(dataReader);
                    viewOrderDTOList.Add(viewOrderDTO);
                }

                return viewOrderDTOList;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ViewOrderDTO> SelectIncomplete()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewOrderSelectIncomplete"))
            {
                List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
                while (dataReader.Read())
                {
                    ViewOrderDTO viewOrderDTO = MapDataReader(dataReader);
                    viewOrderDTOList.Add(viewOrderDTO);
                }

                return viewOrderDTOList;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<ViewOrderDTO> SelectByCustomerAndType(string customerId, int typeId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@CustomerId", customerId),
                new SqlParameter("@TypeId", typeId)
            };
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewOrderSelectByCustomerAndType", parameters))
            {
                List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
                while (dataReader.Read())
                {
                    ViewOrderDTO viewOrderDTO = MapDataReader(dataReader);
                    viewOrderDTO.ReturnAmount = 0;
                    viewOrderDTOList.Add(viewOrderDTO);
                }

                return viewOrderDTOList;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<ViewOrderDTO> SelectByType(int type)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Type", type)
            };
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_ViewOrderSelectByType", parameters))
            {
                List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
                while (dataReader.Read())
                {
                    ViewOrderDTO viewOrderDTO = MapDataReader(dataReader);
                    viewOrderDTOList.Add(viewOrderDTO);
                }

                return viewOrderDTOList;
            }
        }

		/// <summary>
        /// Creates a new instance of the ViewOrderDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
        private ViewOrderDTO MapDataReader(SqlDataReader dataReader)
		{
            ViewOrderDTO viewOrderDTO = new ViewOrderDTO();
            viewOrderDTO.CustomerId = dataReader.GetString("CustomerId", String.Empty);
            viewOrderDTO.CustomerName = dataReader.GetString("CustomerName", String.Empty);
            viewOrderDTO.Address = dataReader.GetString("Address", String.Empty);
            viewOrderDTO.OrderFormId = dataReader.GetString("OrderFormId", String.Empty);
            viewOrderDTO.Building = dataReader.GetString("Building", String.Empty);
            viewOrderDTO.Vat = dataReader.GetInt32("Vat", 0);
            viewOrderDTO.Discount = dataReader.GetInt32("Discount", 0);
            viewOrderDTO.MaterialId = dataReader.GetString("MaterialId", String.Empty);
            viewOrderDTO.MaterialName = dataReader.GetString("MaterialName", String.Empty);
            viewOrderDTO.UnitId = dataReader.GetString("UnitId", null);
            viewOrderDTO.UnitName = dataReader.GetString("UnitName", String.Empty);
            viewOrderDTO.Address = dataReader.GetString("Address", null);
            viewOrderDTO.Amount = dataReader.GetInt32("Amount", 0);
            viewOrderDTO.Remaining = dataReader.GetInt32("Remaining", 0);
            viewOrderDTO.PriceByDay = dataReader.GetInt32("PriceByDay", 0);
            viewOrderDTO.RepositoryId = dataReader.GetString("RepositoryId", String.Empty);
            viewOrderDTO.RentDay = dataReader.GetDateTime("RentDay", new DateTime(0));
            viewOrderDTO.ReturnDay = dataReader.GetDateTime("ReturnDay", new DateTime(0));
            viewOrderDTO.TypeId = dataReader.GetInt32("TypeId", 0);
            return viewOrderDTO;
		}

		#endregion        
    }
}
